﻿using AutoMapper;
using Modules.Constructor;
using Modules.Corp;
using Modules.Project;
using Modules.System.Attachment;
using PmSoft;
using PmSoft.Entity;
using System.ComponentModel.DataAnnotations;

namespace ManagementApi.Models.Manage.ProjectCorp
{
    public class ProjectCorpEditForm
    {

        /// <summary>
        /// 主键 
        ///</summary>
        [Display(Name = "主键")]
        public int? Id { get; set; }
        /// <summary>
        /// 项目ID 
        ///</summary>
        [Display(Name = "项目ID")]
        public int? ProjectId { get; set; }
        /// <summary>
        /// 企业名称
        /// </summary>
        [Display(Name = "企业名称")]
        public string? CorpName { get; set; }

        /// <summary>
        /// 企业ID 
        ///</summary>
        [Display(Name = "企业ID")]
        [Required(ErrorMessage = "{0}不能为空")]
        public int? CorpId { get; set; }
        /// <summary>
        /// 担任角色 
        ///</summary>
        [Display(Name = "担任角色")]
        [Required(ErrorMessage = "{0}不能为空")]
        public 项目参建单位类型? Role { get; set; }

        /// <summary>
        /// 担任角色 
        ///</summary>
        public string? RoleName { get; set; }
        /// <summary>
        /// 承建状态 
        ///</summary>
        [Display(Name = "承建状态")]
        public 承建状态? ConstructionStatus { get; set; }

        /// <summary>
        /// 承建状态 
        ///</summary>
        public string? ConstructionStatusName { get; set; }
        /// <summary>
        /// 入场时间
        ///</summary>
        [Display(Name = "入场时间")]
        [Required(ErrorMessage = "{0}不能为空")]
        public DateTime? StartDate { get; set; }
        /// <summary>
        /// 离场时间
        ///</summary>
        [Display(Name = "离场时间")]
        public DateTime? EndDate { get; set; }

        /// <summary>
        /// 负责人名称
        /// </summary>
        [Display(Name = "负责人名称")]
        public string? LeaderName { get; set; }
        /// <summary>
        /// 负责人身份证号
        /// </summary>
        [Display(Name = "负责人身份证号")]
        public string? LeaderCardno { get; set; }
        /// <summary>
        /// 负责人手机号
        /// </summary>
        [Display(Name = "负责人手机号")]
        public string? LeaderPhoneNum { get; set; }

        /// <summary>
        /// 项目管理部编码
        /// </summary>
        [Display(Name = "项目管理部编码")]
        public string? ProjectManageCode { get; set; }
        /// <summary>
        /// 承建内容
        /// </summary>
        [Display(Name = "承建内容")]
        public string? BuildContent { get; set; }

        /*关联表数据*/

        /// <summary>
        /// 项目名称
        ///</summary>
        [Display(Name = "项目名称")]
        public string? ProjectName { get; set; }
        /// <summary>
        /// 项目编号
        ///</summary>
        [Display(Name = "项目编号")]
        public string? ProjectNum { get; set; }


        /// <summary>
        /// 单位统一社会信用代码
        /// </summary>
        [Display(Name = "单位统一社会信用代码")]
        public string? CorpCode { get; set; }
        /// <summary>
        /// 单位类型
        /// </summary>
        [Display(Name = "单位类型")]
        public ConstructorType? Type { get; set; }

        /// <summary>
        /// 单位类型
        ///</summary>
        public string? TypeName { get; set; }

        /// <summary>
        /// 注册地行政区划代码
        /// </summary>
        [Display(Name = "注册地行政区划代码")]
        public int? AreaCode { get; set; }

        /// <summary>
        /// 注册地行政区划代码
        /// </summary>
        [Display(Name = "注册地行政区划代码")]
        public int? CityCode { get; set; }

        /// <summary>
        /// 注册地行政区划代码
        /// </summary>
        [Display(Name = "注册地行政区划代码")]
        public int? ProvinceCode { get; set; }

        /// <summary>
        /// 办公地址
        /// </summary>
        [Display(Name = "办公地址")]
        public string? OfficeAddr { get; set; }
        /// <summary>
        /// 注册地址
        /// </summary>
        [Display(Name = "注册地址")]
        public string? RegAddr { get; set; }
        /// <summary>
        /// 法人民名称
        /// </summary>
        [Display(Name = "法人民名称")]
        public string? LegalMan { get; set; }
        /// <summary>
        /// 法人身份证号
        /// </summary>
        [Display(Name = "法人身份证号")]
        public string? LegalCardno { get; set; }
        /// <summary>
        /// 法人手机号
        /// </summary>
        [Display(Name = "法人手机号")]
        public string? LegalMobile { get; set; }
        /// <summary>
        /// 合同金额
        /// </summary>
        [Display(Name = "合同金额")]
        public decimal ContractAmount { get; set; }

        /// <summary>
        /// 附件Ids
        /// </summary>
        [Display(Name = "附件Ids")]
        public List<string>? Attachments { get; set; } = new List<string>();

        /// <summary>
        /// 附件列表
        /// </summary>
        [Display(Name = "附件列表")]
        public List<AttachmentInfo>? AttachmentFiles { get; set; } = new List<AttachmentInfo>();


    }
    public static partial class ProjectCorpExceptions
    {
        public static ProjectCorpEditForm AsProjectCorpFrom(this Modules.Project.Entitys.ProjectCorp projectCorp)
        {
            ProjectCorpEditForm projectCorpEditForm = new ProjectCorpEditForm();
            ServiceAccessor.GetService<IMapper>().Map(projectCorp, projectCorpEditForm);
            CorpService corpService = ServiceAccessor.GetService<CorpService>();
            ConstructorService constructorInfoService = ServiceAccessor.GetService<ConstructorService>();
            ProjectService projectService = ServiceAccessor.GetService<ProjectService>();

            if (projectCorpEditForm.ProjectId.HasValue)
            {
                ProjectInfo? projectInfo = projectService.GetProjectInfo((int)projectCorpEditForm.ProjectId);
                if (projectInfo != null)
                {
                    projectCorpEditForm.ProjectName = projectInfo.Name;
                    projectCorpEditForm.ProjectNum = projectInfo.PrjNum;
                }
            }

            var projectCorpAttachmentService = ServiceAccessor.GetService<ProjectAttachmentService>();
            if (projectCorpEditForm.Id.HasValue && projectCorpEditForm.Id > 0)
            {
                projectCorpEditForm.AttachmentFiles = projectCorpAttachmentService.GetSysAttachmentByObjectIdAndType(projectCorpEditForm.Id ?? 0, 附件类型.参建单位合同, 项目附件租户.ProjectCorp).ToList();
            }
 
            CorpInfo? corpInfo = corpService.GetCorpInfo(projectCorp.CorpId);
            if (corpInfo != null)
            {
                projectCorpEditForm.CorpName = corpInfo.Name;
                projectCorpEditForm.CorpCode = corpInfo.Code;
                projectCorpEditForm.Type = corpInfo.Type;
                projectCorpEditForm.AreaCode = corpInfo.AreaCode;
                projectCorpEditForm.OfficeAddr = corpInfo.OfficeAddr;
                projectCorpEditForm.RegAddr = corpInfo.RegAddr;
                projectCorpEditForm.LegalMan = corpInfo.Legalman;
                projectCorpEditForm.LegalCardno = corpInfo.LegalCardno;
                projectCorpEditForm.LegalMobile = corpInfo.LegalMobile;
            }

            projectCorpEditForm.TypeName = projectCorpEditForm.Type?.GetDescription();
            projectCorpEditForm.ConstructionStatusName = projectCorpEditForm.ConstructionStatus?.GetDescription();
            projectCorpEditForm.RoleName = projectCorpEditForm.Role?.GetDescription();
            return projectCorpEditForm;
        }

    }
}
